/** @file    FormDesktopNotifier.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormDesktopNotifier.cpp 2748 2010-07-11 22:07:06Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormDesktopNotifier class.
 * The header for this class can be found in FormDesktopNotifier.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormDesktopNotifier.h"
 #include "ui_FormDesktopNotifier.h"

 #include <core/BusManager.h>
 #include <core/WoshKernel.h>
 #include <core/MethodsCommon.h>
 #include <core/Request.h>
 #include <core/Response.h>
 #include <core/List.h>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormDesktopNotifier, "wosh::gui::IWidgetGeneric", 1.0, _static_FormDesktopNotifier )
 WOSH_GUI_MAP(wosh::gui::FormDesktopNotifier, "wosh::interfaces::services::DesktopNotifier", 0.0, _static_Map_FormDesktopNotifier )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormDesktopNotifier::FormDesktopNotifier(QWidget *parent) : IWidgetGeneric(parent)
 {
	this->m_ui = new Ui::FormDesktopNotifier();
	this->m_ui->setupUi(this);

	// tab Contacts [0]
	this->m_ui->tableViewContacts->setResizeStretch(1);
	connect(this->m_ui->tableViewContacts, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	// tab Notifications [1]
	this->m_ui->tableViewNotifications->setMethodList(_DesktopNotifier_METHOD_listnotif);
	this->m_ui->tableViewNotifications->setResizeStretch(1);
	connect(this->m_ui->tableViewNotifications, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

	// tab Settings [2]
	this->propMapperCfg = new PropertiesMapper(this, true);
	connect(this->propMapperCfg, SIGNAL(send_request(const QString&, ISerializable*)), this, SLOT(executeRemoteMethod(const QString&, ISerializable*)));
	this->propMapperCfg->set(_DesktopNotifier_KEY_DoNotDisturb, this->m_ui->chkDoNotDistrurb);
	this->propMapperCfg->set(_DesktopNotifier_KEY_CacheSize, this->m_ui->spinNotificationCacheSize);
	this->propMapperCfg->set(_DesktopNotifier_KEY_MouseMoveAccurancy, this->m_ui->spinMouseDetectionAccurency);
	connect(this->m_ui->wUpBarSettings, SIGNAL(updateClicked()), this, SLOT(refreshSettings()));

 }

FormDesktopNotifier::~FormDesktopNotifier() {
	delete this->m_ui;
 }

void FormDesktopNotifier::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormDesktopNotifier::refreshAll() {
	this->setWindowTitle( QString("DesktopNotifier [%1]").arg(this->getTargetURI().getKernelName().c_str()) );
	this->m_ui->tableViewContacts->refreshData();
 	this->m_ui->tableViewNotifications->refreshData();
	refreshSettings();
 }

void FormDesktopNotifier::refreshSettings() {
	List* dl = new List();
	dl->push_back<Variant>(_DesktopNotifier_KEY_DoNotDisturb);
	dl->push_back<Variant>(_DesktopNotifier_KEY_CacheSize);
	dl->push_back<Variant>(_DesktopNotifier_KEY_MouseMoveAccurancy);
	executeRemoteMethod( _METHOD_GetProperties, dl );
	this->m_ui->wUpBarSettings->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormDesktopNotifier::busMessage( const Message& message, const Bus* ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) ) return;
	const wosh::Response* response = message.getContent()->asResponse();

	this->m_ui->tableViewContacts->evalResponse(response);
	this->m_ui->tableViewNotifications->evalResponse(response);

	if ( this->propMapperCfg->evalResponse(response) )
		this->m_ui->wUpBarSettings->sayReady();

 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
